package com.waymap.service.impl;


import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;


import com.github.pagehelper.PageHelper;
import com.waymap.Thread.WmsThread;

import com.waymap.Thread.WmsThreads;
import com.waymap.domain.*;
import com.waymap.mapper.*;

import com.waymap.utils.NewNo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.waymap.common.vo.WaveVo;
import com.waymap.service.TransactionsService;
import com.waymap.utils.CommonEnum;
import com.waymap.utils.CommonService;
import com.waymap.utils.mq.MsgProducer;

@Service
public class TransactionsServiceImpl extends BaseService<Transactions> implements TransactionsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TransactionsMapper transactionsMapper;
    @Resource
    private TransactionItemsMapper transactionItemsMapper;
    @Resource
    private ItemInventorysMapper itemInventorysMapper;
    @Resource
    private TransactionItemInventoryDetailsMapper transactionItemInventoryMapper;
    @Resource
    private ItemsMapper itemsMapper;
    @Resource
    private NewNo newNo;

    @Autowired
    private MsgProducer producer;

    @Resource
    private UserMapper userMapper;


    @Resource
    private AccountsMapper accountsMapper;

    @Resource
    private CommonService commonService;


    /**
     * @return
     * @author vincent mai
     * 采购入库--查询单号列表
     */
    @Override
    public List<Map<String, Object>> getTranidList(Map<String, Object> param) {
        return transactionsMapper.getTranidList(param);
    }

    @Override
    public List<Map<String, Object>> searchICItem(Map<String, Object> param) {
        return transactionsMapper.searchICItem(param);
    }

    @Override
    public Map<String, Object> getTranidStatus(String tranid, String accountId) {
        Map<String, Object> map = new HashMap(16);
        Transactions transactions = new Transactions();
        transactions.setTranid(tranid);
        transactions.setAccountId(accountId);
        Transactions transactions1 = transactionsMapper.selectOne(transactions);
        if (transactions1==null){
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
        }else{
            map.put("result", transactions1);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
        }
        return map;
    }

    /**
     * getTransationDetailList2
     *
     * @param param
     * @return
     * @author vincent mai
     * 获取波次号
     */
    @Override
    public ResponseEntity<Map> getWaves(Map<String, Object> param) {
        Map<String, Object> map = new HashMap(16);
        List<WaveVo> list = new ArrayList<>();
        List<Map<String, Object>> transactionsList = transactionsMapper.getWaveCode(param);
        if (null != transactionsList && transactionsList.size() > 0) {
            for (Map<String, Object> ob : transactionsList) {
                WaveVo wave = new WaveVo();
                wave.setStatus(ob.get("status").toString());
                wave.setWaveCode(ob.get("waveCode").toString());
                param.put("waveCode", ob.get("waveCode").toString());
                List<Map<String, Object>> trList = transactionsMapper.getTransationId(param);
                if (null != trList && trList.size() > 0) {
                    for (Map<String, Object> obTransation : trList) {
                        /**总数量**/
                        int quantity = Integer.parseInt(obTransation.get("quantity").toString());
                        /**发货数量*/
                        int quantityFulfilled = Integer.parseInt(obTransation.get("quantityFulfilled").toString());
                        if (quantity == quantityFulfilled) {
                            obTransation.put("orderStatus", "已完成");
                        } else if (quantity > quantityFulfilled) {
                            obTransation.put("orderStatus", "进行中");
                        } else {
                            obTransation.put("orderStatus", "未开始");
                        }

                    }
                    wave.setOrderList(trList);
                }
                list.add(wave);
            }
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
            return ResponseEntity.ok(map);
        }
        map.put("result", list);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    /**
     * 获取账户信息
     *
     * @param param
     * @return
     */
    @Override
    public Accounts getAccountInfo(Map<String, Object> param) {
        return accountsMapper.getAccountInfo(param);
    }

    /**
     * 更新syncToNs状态
     *
     * @param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatusByTranid(Map<String, Object> param) {
        transactionsMapper.updateStatusByTranid(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserByTranid(Map<String, Object> param) {
        transactionsMapper.updateUserByTranid(param);
    }

    @Override
    public void updateStatusByError(Map<String, Object> param) {
        transactionsMapper.updateStatusByError(param);
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean insertTrackingNumberInfo(Map<String, Object> param, String tranid, String accountId) {

        List<Transactions> tranList = transactionsMapper.queryTransactionList(param);
        List<Map<String, Object>> listDetails = transactionItemsMapper.getAllTransactionItemsByTrackingNumber(param);

        Transactions trOb = new Transactions();
        if (tranList != null && tranList.size() > 0) {
            Transactions transaction = tranList.get(0);
            trOb.setTranid(tranid);
            trOb.setStatus(transaction.getStatus());
            trOb.setTransactionType("TRACKING_NUMBER");
            trOb.setCreatedFromType(transaction.getTransactionType());
            trOb.setAccountId(transaction.getAccountId());
            trOb.setSyncToNs("N");
            trOb.setLocationId(transaction.getLocationId());
            trOb.setLocationName(transaction.getLocationName());
            trOb.setToLocationId(transaction.getToLocationId());
            trOb.setToLocationName(transaction.getToLocationName());
            trOb.setTrackingNumber("");
            trOb.setCreatedBy("TEST");
            trOb.setNsInternalid(transaction.getNsInternalid());
            trOb.setLastUpdateBy("TEST");
            // 用于拼凑tranid
            String createFrom = "";
            for (int i = 0; i < tranList.size(); i++) {
                if (i == 0) {
                    createFrom = tranList.get(i).getTranid();
                } else {
                    createFrom += ":" + tranList.get(i).getTranid();
                }
            }
            trOb.setCreatedFrom(createFrom);
            int tranCount = transactionsMapper.insert(trOb);
            if (tranCount <= 0) {
                return false;
            }
        }


        if (listDetails != null && listDetails.size() > 0) {
            for (Map<String, Object> itemMap : listDetails) {
                TransactionItems tailOb = new TransactionItems();
                tailOb.setItemId((Integer) itemMap.get("itemId"));
                tailOb.setQuantity(Integer.parseInt(itemMap.get("quantity").toString()));
                tailOb.setQuantityFulfilled(Integer.parseInt(itemMap.get("quantityFulfilled").toString()));
                tailOb.setQuantityReceived(Integer.parseInt(itemMap.get("quantityReceived").toString()));
                tailOb.setBinId((Integer) itemMap.get("binId"));
                tailOb.setBinNumber((String) itemMap.get("binNumber"));
                tailOb.setTransactionId(tranid);
                int tranDetailCount = transactionItemsMapper.insert(tailOb);
                if (tranDetailCount <= 0) {
                    return false;
                }
            }
        }

        //更新找到的TRANSACTIONS成COMPLETE，避免重复处理
        for (Transactions tx : tranList) {
            tx.setStatus("COMPLETE");
            transactionsMapper.updateByPrimaryKey(tx);
        }
        /*****利用线程发送ACTIVE_MQ模块*****/
        WmsThread wms = new WmsThread("wms-mq", tranid, "TRACKING_NUMBER", producer, "TRACKING_NUMBER", accountId);
        wms.start();

        return true;
    }

    @Override
    public Map<String, Object> selectToWaveList(String tranid, String transactionType, String accountId) {
        Map<String, Object> param = new HashMap(16);
        Map<String, Object> map = new HashMap(16);
        param.put("accountId", accountId);
        param.put("tranid", tranid);
        param.put("transaction_type", transactionType);
        List<Map<String, Object>> transactionslist = transactionsMapper.getTransationDetailList3(param);
        if (null != transactionslist && transactionslist.size() > 0) {
            for (Map trans : transactionslist) {
                int quantity = (int) trans.get("quantity");
                int quantityFulfilled = (int) trans.get("quantityFulfilled");
                int q = quantity - quantityFulfilled;
                TransactionItemInventoryDetails transactionItemInventoryDetails = new TransactionItemInventoryDetails();
                transactionItemInventoryDetails.setTransactionId(tranid);
                transactionItemInventoryDetails.setItemId((Integer) trans.get("itemId"));
                transactionItemInventoryDetails.setStatus("PENDING");
                transactionItemInventoryDetails.setAccountId(accountId);
                List<TransactionItemInventoryDetails> select = transactionItemInventoryMapper.select(transactionItemInventoryDetails);
                for (TransactionItemInventoryDetails t : select) {
                    q = q - t.getQuantity();
                }
                if (("PO_RETURN".equals(transactionType)) || ("TO_WAVE".equals(transactionType)) || ("SO_WAVE".equals(transactionType))) {
                    trans.put("result", q);
                }
            }
        }
        map.put("result", transactionslist);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return map;
    }

    @Override
    public Map<String, Object> selectSTList(String tranid, String orderNumber, String accountId) {
        Map<String, Object> param = new HashMap(16);
        Map<String, Object> param1 = new HashMap(16);
        Map<String, Object> map = new HashMap(16);
        param1.put("accountId", accountId);
        param.put("accountId", accountId);
        param1.put("tranid", tranid);
        param.put("tranid", tranid);
        param.put("quantityReceived", 1);
        if (orderNumber != null && !"".equals(orderNumber)) {
            TransactionItems transactionItems = new TransactionItems();
            transactionItems.setTransactionId(tranid);
            transactionItems.setAccountId(accountId);
            transactionItems.setOrderNumber(orderNumber);
            List<TransactionItems> select = transactionItemsMapper.select(transactionItems);
            for (TransactionItems t : select) {
                t.setQuantityReceived(1);
                transactionItemsMapper.updateByPrimaryKey(t);
            }
        }
        List<TransactionItems> list1 = transactionItemsMapper.getSTTransactionItems(param1);
        List<TransactionItems> list = transactionItemsMapper.getSTTransactionItems(param);
        if (list.size() == list1.size() && list.size() > 0) {
            Transactions transactions = new Transactions();
            transactions.setTranid(tranid);
            transactions.setAccountId(accountId);
            Transactions transactions1 = transactionsMapper.selectOne(transactions);
            transactions1.setStatus("COMPLETE");
            transactionsMapper.updateByPrimaryKey(transactions1);
        }
        Transactions transactions = new Transactions();
        transactions.setAccountId(accountId);
        transactions.setTranid(tranid);
        Transactions transactions1 = transactionsMapper.selectOne(transactions);
        if (transactions1 != null) {
            map.put("username", transactions1.getLockedByUser());
            map.put("orderList", list1);
            map.put("sum", list1.size());
            map.put("scaned", list.size());
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
        }

        return map;
    }

    @Override
    public Map<String, Object> selectWavePickUp(String tranid, String pickup, String accountId, int location) {
        Map<String, Object> map = new HashMap(16);
        Transactions transactions = new Transactions();
        transactions.setAccountId(accountId);
        transactions.setTranid(tranid);
        transactions.setLocationId(location);
        transactions.setStatus("PENDING");
        transactions.setStatus1("Pending for Pick");
        transactions.setTransactionType("SO_WAVE");
        Transactions transactions1 = transactionsMapper.selectOne(transactions);
        if (transactions1 == null) {
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", "单号不存在");
            return map;
        } else {
            if (pickup != null && !"".equals(pickup)) {
                Transactions transactions2 = new Transactions();
                transactions2.setTransactionType("SO_WAVE");
                transactions2.setAccountId(accountId);
                transactions2.setPickup(pickup);
                if (transactionsMapper.selectOne(transactions2) == null) {
                    transactions1.setPickup(pickup);
                    transactionsMapper.updateByPrimaryKey(transactions1);
                    System.out.println(transactions1);

                    WmsThread wms = new WmsThread("wms-mq", tranid, transactions1.getTransactionType(), producer, "SO_WAVE", accountId);
                    wms.start();

                    map.put("status", CommonEnum.value("eSuccess"));
                    map.put("msg", "绑定拣选框成功");
                    return map;
                } else {
                    map.put("status", CommonEnum.value("eSuccess"));
                    map.put("msg", "此拣选框已被占用");
                    return map;
                }
            }
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", "请输入拣选框");
            return map;
        }
    }

    @Override
    public Map<String, Object> getShippingCarrier(String trackingNumber, String accountId) {
        Map<String, Object> map = new HashMap(16);
        Transactions transactions = new Transactions();
        transactions.setTrackingNumber(trackingNumber);
        transactions.setAccountId(accountId);
        transactions.setStatus("PENDING");
        List<Transactions> select = transactionsMapper.select(transactions);
        if (select.size() == 0) {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
            return map;
        }
        map.put("transactions", select.get(0));
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return map;
    }

    /**
     * 封装transation数据
     *
     * @param tranid
     * @param transactionType
     * @param orderNumber
     * @param location
     * @param
     * @param
     * @return
     */
    @Override
    public Map<String, Object> processTransation(String tranid, String transactionType, String orderNumber, String location, String accountId, String entry) {
        Map<String, Object> map = new HashMap(16);
        Map<String, Object> param = new HashMap(16);
        param.put("accountId", accountId);
        param.put("tranid", tranid);
        param.put("transactionType", transactionType);
        param.put("orderNumber", orderNumber);
        List<Map<String, Object>> transactionslist = transactionsMapper.getTransationDetailList2(param);
        if (null != transactionslist && transactionslist.size() > 0) {
            for (Map trans : transactionslist) {
                int quantity = (int) trans.get("quantity");
                int quantityReceived = (int) trans.get("quantityReceived");
                int quantityFulfilled = (int) trans.get("quantityFulfilled");
                int quantityScaned = (int) trans.get("quantityScaned");
                /**追踪号,发货计划,采购入库PO,销售退货**/
                if (("TRACKING_NUMBER".equals(transactionType)) || ("BIN_TRANSFER_TASK".equals(transactionType))
                        || ("PO".equals(transactionType)) || ("SO_RETURN".equals(transactionType))
                        || ("INVENTORY_COUNT".equals(transactionType))
                        || ("ITEM_RECEIPT_PLAN".equals(transactionType))) {
                    trans.put("result", quantity - quantityReceived);
                } else if (("PO_RETURN".equals(transactionType)) || ("TO_WAVE".equals(transactionType)) || ("SO_WAVE".equals(transactionType))) {
                    trans.put("result", quantity - quantityFulfilled);
                } else if ("TO".equals(transactionType) || ("SO".equals(transactionType))) {
                    int locationId = (int) trans.get("locationId");
                    if (Integer.parseInt(location) == locationId) {
                        trans.put("result", quantity - quantityFulfilled);
                    } else {
                        trans.put("result", quantity - quantityReceived);
                    }
                }
            }
            map.put("result", transactionslist);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
        } else {
            /**针对任务号特殊处理**/
            if ("TRACKING_NUMBER".equals(transactionType)) {
                param.put("trackingNumber", tranid);
                //当入口是TO时，只合并当前仓库为收货仓库的TO，出货的TO不做合并处理
                if ("TO".equals(entry)) {
                    param.put("toLocationId", location);
                }
                /**如果单子是TRACKING_NUMBER,就合并单子,用TRACKING_NUMBER号作为tranID处理**/
                boolean success = insertTrackingNumberInfo(param, tranid, accountId);
                if (success == false) {
                    map.put("status", CommonEnum.value("eNotFind"));
                    map.put("msg", CommonEnum.value("notFind"));
                    return map;
                }
                param.clear();
                param.put("accountId", accountId);
                param.put("tranid", tranid);
                param.put("transactionType", transactionType);
                param.put("orderNumber", orderNumber);
                List<Map<String, Object>> translist = transactionsMapper.getTransationDetailList2(param);
                if (null != translist && translist.size() > 0) {
                    for (Map tr : translist) {
                        int quantity = (int) tr.get("quantity");
                        int quantityReceived = (int) tr.get("quantityReceived");
                        tr.put("result", quantity - quantityReceived);
                    }
                }
                map.put("result", translist);
                map.put("status", CommonEnum.value("eSuccess"));
                map.put("msg", CommonEnum.value("eSMessage"));
            } else {
                map.put("status", CommonEnum.value("eNotFind"));
                map.put("msg", CommonEnum.value("notFind"));
            }
        }
        return map;
    }


    /**
     * @author vincent mai
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Map> submitTransaction(Map request, User currentUser) {
        // TODO Auto-generated method stub
        //创建收货单或发货单
        Map<String, Object> param = new HashMap();
        param.put("tranid", request.get("transactionId"));
        param.put("accountId", request.get("accountId"));
        param.put("transactionType", request.get("transactionType"));
        Transactions trs = transactionsMapper.getTransactionByTranid(param);
        String transType = trs.getTransactionType();
        int location = (Integer) request.get("location");

        if ("INVENTORY_COUNT".equals(transType)) {
            Date d = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateNowStr1 = sdf1.format(d);
            trs.setOperateTime(dateNowStr1);
            transactionsMapper.updateByPrimaryKey(trs);
            TransactionItemInventoryDetails tiid = new TransactionItemInventoryDetails();
            tiid.setTransactionId((String) request.get("transactionId"));
            tiid.setStatus("PENDING");
            tiid.setAccountId((String) request.get("accountId"));
            List<TransactionItemInventoryDetails> tiids = transactionItemInventoryMapper.select(tiid);
            for (TransactionItemInventoryDetails tmp : tiids) {
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.updateByPrimaryKey(tmp);
            }
            String tranid = (String) request.get("transactionId");
            String account = (String) request.get("accountId");
            WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, transType, account);
            wms.start();

            trs.setStatus("COMPLETE");
            transactionsMapper.updateByPrimaryKey(trs);

            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
            return ResponseEntity.ok(map);
        }

        if (("TRACKING_NUMBER".equals(transType) && "PO".equals(trs.getCreatedFromType()))
                || ("TRACKING_NUMBER".equals(transType) && "TO".equals(trs.getCreatedFromType())
                && location == trs.getToLocationId())
                || ("ITEM_RECEIPT_PLAN".equals(transType)) || ("PO".equals(transType))
                || ("SO_RETURN".equals(transType)) || (("TO".equals(transType) && location == trs.getToLocationId()))) {
            /**收货计划或采购入库 或销售退货 或调拨单**/
            List<TransactionItems> trsDetails = transactionItemsMapper.getAllTransactionItems(param);
            TransactionItemInventoryDetails tiid = new TransactionItemInventoryDetails();
            tiid.setTransactionId((String) request.get("transactionId"));
            tiid.setStatus("PENDING");
            tiid.setAccountId((String) request.get("accountId"));
            List<TransactionItemInventoryDetails> tiids = transactionItemInventoryMapper.select(tiid);

            for (TransactionItemInventoryDetails tmp : tiids) {
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.updateByPrimaryKey(tmp);
            }
            createItemReceipt(transType, trs.getTranid(), trsDetails, tiids, trs.getAccountId(), location, currentUser);

            if ("ITEM_RECEIPT_PLAN".equals(transType)) {
                trs.setStatus("COMPLETE");
                transactionsMapper.updateByPrimaryKey(trs);
            }
            List<TransactionItems> trsDetails1 = transactionItemsMapper.getAllTransactionItems(param);
            boolean completed = false;
            for (TransactionItems detail : trsDetails1) {
                if (detail.getQuantity().equals(detail.getQuantityReceived())) {
                    completed = true;
                } else {
                    completed = false;
                    break;
                }
            }
            if (completed) {
                trs.setStatus("COMPLETE");
                transactionsMapper.updateByPrimaryKey(trs);
            }
        } else if (("WAVE".equals(transType) && !"".equals(request.get("orderNumber")))
                || ("TRACKING_NUMBER".equals(transType) && location == trs.getLocationId())) {
            /**采购退货 或(波号)或调拨单**/
            param.put("orderNumber", request.get("orderNumber"));
            List<TransactionItems> trsDetails = transactionItemsMapper.getAllTransactionItems(param);
            TransactionItemInventoryDetails tiid = new TransactionItemInventoryDetails();
            tiid.setTransactionId((String) request.get("transactionId"));
            tiid.setStatus("PENDING");
            tiid.setAccountId((String) request.get("accountId"));
            List<TransactionItemInventoryDetails> tiids = transactionItemInventoryMapper.select(tiid);
            if ("WAVE".equals(transType) && !"".equals(request.get("orderNumber"))) {
                createItemFulfillment(transType, (String) request.get("orderNumber"), trsDetails, tiids, trs.getAccountId(), request, currentUser);
            } else {
                createItemFulfillment(transType, trs.getTranid(), trsDetails, tiids, trs.getAccountId(), request, currentUser);
            }
        } else if ("TO_WAVE".equals(transType) || "SO_WAVE".equals(transType) || "PO_RETURN".equals(transType)) {
            List<TransactionItems> trsDetails = transactionItemsMapper.getAllTransactionItems(param);
            TransactionItemInventoryDetails tiid = new TransactionItemInventoryDetails();
            tiid.setTransactionId((String) request.get("transactionId"));
            tiid.setStatus("PENDING");
            tiid.setAccountId((String) request.get("accountId"));
            List<TransactionItemInventoryDetails> tiids = transactionItemInventoryMapper.select(tiid);

            for (TransactionItemInventoryDetails tmp : tiids) {
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.updateByPrimaryKey(tmp);
            }
            createItemFulfillment(transType, trs.getTranid(), trsDetails, tiids, trs.getAccountId(), request, currentUser);

            List<TransactionItems> trsDetails1 = transactionItemsMapper.getAllTransactionItems(param);
            boolean completed = false;
            for (TransactionItems detail : trsDetails1) {
                if (detail.getQuantity().equals(detail.getQuantityFulfilled())) {
                    completed = true;
                } else {
                    completed = false;
                    break;
                }
            }
            if (completed) {
                trs.setStatus("COMPLETE");
                transactionsMapper.updateByPrimaryKey(trs);
            }
        } else if ("BIN_TRANSFER_TASK".equals(transType)) {
            List<TransactionItems> trsDetails = transactionItemsMapper.getAllTransactionItems(param);
            TransactionItemInventoryDetails tiid = new TransactionItemInventoryDetails();
            tiid.setTransactionId((String) request.get("transactionId"));
            tiid.setStatus("PENDING");
            tiid.setAccountId((String) request.get("accountId"));
            List<TransactionItemInventoryDetails> tiids = transactionItemInventoryMapper.select(tiid);

            for (TransactionItemInventoryDetails tmp : tiids) {
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.updateByPrimaryKey(tmp);
            }
            createBinTransfer(transType, trs.getTranid(), trsDetails, tiids, trs.getAccountId(), location, request, currentUser);

            List<TransactionItems> trsDetails1 = transactionItemsMapper.getAllTransactionItems(param);
            boolean completed = false;
            for (TransactionItems detail : trsDetails1) {
                if (detail.getQuantity().equals(detail.getQuantityReceived())) {
                    completed = true;
                } else {
                    completed = false;
                    break;
                }
            }
            if (completed) {
                trs.setStatus("COMPLETE");
                transactionsMapper.updateByPrimaryKey(trs);
            }
        }
        Map<String, Object> map = new HashMap(16);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @Override
    public ResponseEntity<Map> submitNoIRTNumber(Map request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        String tranid = "NOIRT_" + dateNowStr;
        String transType = (String) request.get("transType");
        String accountId = (String) request.get("accountId");
        Transactions transactions = new Transactions();
        transactions.setTranid(tranid);
        transactions.setCreatedFrom((String) request.get("createdFrom"));
        transactions.setTransactionType(transType);
        transactions.setAccountId(accountId);
        transactions.setLocationId((Integer) request.get("location"));
        transactions.setCreatedBy("TEST");
        transactions.setLastUpdateBy("TEST");
        transactions.setStatus("COMPLETE");
        transactions.setSyncToNs("N");
        int tid = transactionsMapper.insert(transactions);
        Map<String, Object> map = new HashMap(16);
        if (tid > 0) {
            TransactionItems transactionItems = new TransactionItems();
            transactionItems.setTransactionId(tranid);
            transactionItems.setItemId((Integer) request.get("itemId"));
            transactionItems.setAccountId(accountId);
            transactionItems.setQuantity((Integer) request.get("quantity"));
            transactionItems.setQuantityReceived(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setQuantityScaned(0);
            if (transactionItemsMapper.insert(transactionItems) > 0) {
                TransactionItemInventoryDetails transactionItemInventoryDetails = new TransactionItemInventoryDetails();
                transactionItemInventoryDetails.setTransactionId(tranid);
                transactionItemInventoryDetails.setAccountId(accountId);
                transactionItemInventoryDetails.setItemId((Integer) request.get("itemId"));
                transactionItemInventoryDetails.setBinNumber((String) request.get("binNumber"));
                transactionItemInventoryDetails.setToBinNumber((String) request.get("toBinNumber"));
                transactionItemInventoryDetails.setQuantity((Integer) request.get("quantity"));
                transactionItemInventoryDetails.setStatus("COMPLETE");
                int id = transactionItemInventoryMapper.insert(transactionItemInventoryDetails);
                if (id > 0) {
                    //修改库存数量，from bin做减法，to bin做加法
                    //commonService.updateItemInventory((Integer) request.get("location"),(Integer) request.get("itemId"),accountId,(String) request.get("binNumber"),null,-(Integer) request.get("quantity"));
                    //commonService.updateItemInventory((Integer) request.get("location"),(Integer) request.get("itemId"),accountId,(String) request.get("toBinNumber"),null,(Integer) request.get("quantity"));

                    WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, "BIN_TRANSFER", accountId);
                    wms.start();
                    map.put("status", CommonEnum.value("eSuccess"));
                    map.put("msg", CommonEnum.value("eSMessage"));
                } else {
                    map.put("status", CommonEnum.value("eNotFind"));
                    map.put("msg", "获取transactionId为空,MQ推送失败");
                }
            }
        }
        return ResponseEntity.ok(map);
    }

    @Override
    public ResponseEntity<Map> submitTrackingNumber(Map request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        String tranid = "ST_" + dateNowStr;
        String transType = (String) request.get("transType");
        String accountId = (String) request.get("accountId");
        Transactions transactions = new Transactions();
        transactions.setTranid(tranid);
        transactions.setCreatedFrom((String) request.get("createdFrom"));
        transactions.setTransactionType(transType);
        transactions.setAccountId(accountId);
        transactions.setLocationId((Integer) request.get("location"));
        transactions.setCreatedBy((String) request.get("username"));
        transactions.setLastUpdateBy((String) request.get("username"));
        transactions.setStatus("COMPLETE");
        transactions.setSyncToNs("N");
        transactionsMapper.insert(transactions);
        List<Map<String, Object>> list = (List<Map<String, Object>>) request.get("list");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            TransactionItems transactionItems = new TransactionItems();
            transactionItems.setTransactionId(tranid);
            transactionItems.setAccountId((String) request.get("accountId"));
            transactionItems.setItemId(1);
            transactionItems.setQuantity(0);
            transactionItems.setQuantityReceived(0);
            transactionItems.setQuantityScaned(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setOrderNumber((String) list.get(i).get("order"));
            String border = list.get(i).get("Length") + "|" + list.get(i).get("Width") + "|" + list.get(i).get("Heighth");
            transactionItems.setLotNumber(border);
            transactionItemsMapper.insert(transactionItems);

            Transactions tracking = new Transactions();
            tracking.setTrackingNumber((String) list.get(i).get("order"));
            tracking.setAccountId(accountId);
            List<Transactions> select = transactionsMapper.select(tracking);
            for (Transactions s : select) {
                s.setStatus("COMPLETE");
                s.setLockedByUser((String) request.get("username"));
                s.setLastUpdateBy((String) request.get("username"));
                transactionsMapper.updateByPrimaryKey(s);
            }
        }
        WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, transType, accountId);
        wms.start();
        Map<String, Object> map = new HashMap(16);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @Override
    public ResponseEntity<Map> submitToShippingMark(Map request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        String tranid = "TOMARK_" + dateNowStr;
        String transType = (String) request.get("transType");
        String accountId = (String) request.get("accountId");
        Transactions transactions = new Transactions();
        transactions.setTranid(tranid);
        transactions.setCreatedFrom((String) request.get("createdFrom"));
        transactions.setTransactionType(transType);
        transactions.setAccountId(accountId);
        transactions.setLocationId((Integer) request.get("location"));
        transactions.setCreatedBy("TEST");
        transactions.setLastUpdateBy("TEST");
        transactions.setStatus("COMPLETE");
        transactions.setSyncToNs("N");
        transactionsMapper.insert(transactions);
        List list = (List) request.get("list");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            TransactionItems transactionItems = new TransactionItems();
            transactionItems.setTransactionId(tranid);
            transactionItems.setAccountId((String) request.get("accountId"));
            transactionItems.setItemId(1);
            transactionItems.setQuantity(0);
            transactionItems.setQuantityReceived(0);
            transactionItems.setQuantityScaned(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setOrderNumber((String) list.get(i));
            transactionItemsMapper.insert(transactionItems);
        }
        WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, transType, accountId);
        wms.start();
        Map<String, Object> map = new HashMap(16);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @Override
    public ResponseEntity<Map> creatInventoryAdjustment(Map request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        String tranid = "InventoryAdj_" + dateNowStr;
        String transType = "NO_INVENTORY_COUNT";
        String accountId = (String) request.get("accountId");
        Transactions transactions = new Transactions();
        transactions.setTranid(tranid);
        transactions.setCreatedFrom((String) request.get("createdFrom"));
        transactions.setTransactionType(transType);
        transactions.setAccountId(accountId);
        transactions.setLocationId((Integer) request.get("location"));
        transactions.setCreatedBy((String) request.get("userName"));
        transactions.setLastUpdateBy((String) request.get("userName"));
        transactions.setStatus("COMPLETE");
        transactions.setStatus1("Pending");
        transactions.setSyncToNs("N");
        int tid = transactionsMapper.insert(transactions);
        Map<String, Object> map = new HashMap(16);
        if (tid > 0) {
            TransactionItems transactionItems = new TransactionItems();
            transactionItems.setTransactionId(tranid);
            transactionItems.setItemId((Integer) request.get("itemId"));
            transactionItems.setAccountId(accountId);
            transactionItems.setQuantity((Integer) request.get("quantity"));
            transactionItems.setQuantityReceived(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setQuantityFulfilled(0);
            transactionItems.setQuantityScaned(0);
            transactionItems.setBinNumber((String) request.get("binNumber"));
            transactionItems.setToBinNumber((String) request.get("toBinNumber"));
            if (transactionItemsMapper.insert(transactionItems) > 0) {
                TransactionItemInventoryDetails transactionItemInventoryDetails = new TransactionItemInventoryDetails();
                transactionItemInventoryDetails.setTransactionId(tranid);
                transactionItemInventoryDetails.setAccountId(accountId);
                transactionItemInventoryDetails.setItemId((Integer) request.get("itemId"));
                transactionItemInventoryDetails.setBinNumber((String) request.get("binNumber"));
                transactionItemInventoryDetails.setToBinNumber((String) request.get("toBinNumber"));
                transactionItemInventoryDetails.setQuantity((Integer) request.get("quantity"));
                transactionItemInventoryDetails.setStatus("COMPLETE");
                int id = transactionItemInventoryMapper.insert(transactionItemInventoryDetails);
                if (id > 0) {
                    WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, transType, accountId);
                    wms.start();
                    map.put("status", CommonEnum.value("eSuccess"));
                    map.put("msg", CommonEnum.value("eSMessage"));
                } else {
                    map.put("status", CommonEnum.value("eNotFind"));
                    map.put("msg", "获取transactionId为空,MQ推送失败");
                }
            }
        }
        return ResponseEntity.ok(map);
    }

    private void createBinTransfer(String transType, String tranId, List<TransactionItems> trsDetails, List<TransactionItemInventoryDetails> tiids, String accountId, int location, Map request, User currentUser) {
        Transactions binTransfer = new Transactions();
        binTransfer.setCreatedFrom(tranId);
        binTransfer.setCreatedFromType(transType);
        binTransfer.setCreatedBy(currentUser.getUserName());
        binTransfer.setLastUpdateBy(currentUser.getUserName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        binTransfer.setTranid(tranId + "_BIN_TRANSFER_" + dateNowStr);

        binTransfer.setTransactionType("BIN_TRANSFER");
        binTransfer.setAccountId(accountId);
        binTransfer.setSyncToNs("N");
        binTransfer.setNum(0);
        Map<String, Object> param = new HashMap();
        param.put("tranid", request.get("transactionId"));
        param.put("accountId", request.get("accountId"));
        Transactions trs = transactionsMapper.getTransactionByTranid(param);
        binTransfer.setLocationId(trs.getLocationId());
        binTransfer.setLocationName(trs.getLocationName());
        binTransfer.setStatus("COMPLETE");
        int resultCount = transactionsMapper.insert(binTransfer);
        /*********************只有事务插入成功才往下执行**************************/
        if (resultCount > 0) {
            logger.info("transactions新增成功");
            for (TransactionItems detail : trsDetails) {
                if (detail.getQuantityScaned() > 0) {
                    detail.setQuantityReceived(detail.getQuantityReceived() + detail.getQuantityScaned());
                    detail.setQuantityScaned(0);
                    transactionItemsMapper.updateByPrimaryKey(detail);
                    TransactionItems line = new TransactionItems();
                    line.setTransactionId(tranId + "_BIN_TRANSFER_" + dateNowStr);
                    line.setLotNumber(detail.getLotNumber());
                    line.setItemId(detail.getItemId());

                    Map<String, Object> param2 = new HashMap();
                    param2.put("tranid", tranId);
                    int itemId = detail.getItemId();
                    param2.put("itemId", itemId);
                    int quantity = 0;
                    for (TransactionItemInventoryDetails tiid : tiids) {
                        if (itemId == tiid.getItemId()) {
                            quantity += tiid.getQuantity();
                        }
                    }
                    param2.put("accountId", accountId);
                    line.setQuantity(quantity);
                    line.setQuantityFulfilled(0);
                    line.setQuantityReceived(0);
                    line.setQuantityScaned(0);
                    line.setBinNumber(detail.getBinNumber());
                    line.setAccountId(detail.getAccountId());
                    int result = transactionItemsMapper.insert(line);
                    if (result > 0) {
                        logger.info("transactionItems新增成功");
                    }
                }
            }

            for (TransactionItemInventoryDetails tiid : tiids) {
                TransactionItemInventoryDetails tmp = new TransactionItemInventoryDetails();
                tmp.setLotNumber(tiid.getLotNumber());
                tmp.setAccountId(tiid.getAccountId());
                tmp.setTransactionId(tranId + "_BIN_TRANSFER_" + dateNowStr);
                tmp.setItemId(tiid.getItemId());
                tmp.setBinNumber(tiid.getBinNumber());
                tmp.setToBinNumber(tiid.getToBinNumber());
                tmp.setQuantity(tiid.getQuantity());
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.insert(tmp);
                logger.info("transactionInventoryDetails新增成功");
                //修改库存数量，from bin做减法，to bin做加法
                //commonService.updateItemInventory(location,tiid.getItemId(),tiid.getAccountId(),tiid.getBinNumber(),tiid.getLotNumber(),-tiid.getQuantity());
                //commonService.updateItemInventory(location,tiid.getItemId(),tiid.getAccountId(),tiid.getToBinNumber(),tiid.getLotNumber(),tiid.getQuantity());
            }
            /*****利用线程发送ACTIVE_MQ模块*****/
            String transactionId = tranId + "_BIN_TRANSFER_" + dateNowStr;
            WmsThread wms = new WmsThread("wms-mq", transactionId, transType, producer, "BIN_TRANSFER", accountId);
            wms.start();
        }
    }

    private void createItemReceipt(String transType, String tranId, List<TransactionItems> trsDetails, List<TransactionItemInventoryDetails> tiids, String accountId, int location, User currentUser) {
        Transactions itemReceipt = new Transactions();
        itemReceipt.setCreatedFrom(tranId);
        itemReceipt.setCreatedFromType(transType);
        itemReceipt.setCreatedBy(currentUser.getUserName());
        itemReceipt.setLocationId(location);
        itemReceipt.setLastUpdateBy(currentUser.getUserName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(new Date());
        itemReceipt.setTranid(tranId + "_ITEM_RECEIPT_" + dateNowStr);

        itemReceipt.setTransactionType("ITEM_RECEIPT");
        itemReceipt.setAccountId(accountId);
        itemReceipt.setSyncToNs("N");
        itemReceipt.setNum(0);
        int resultCount = transactionsMapper.insert(itemReceipt);
        /*********************只有事务插入成功才往下执行**************************/
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyMMddHHmm");
        String dateNowStr1 = sdf1.format(new Date());
        if (resultCount > 0) {
            logger.info("transactions新增成功");
            for (TransactionItems detail : trsDetails) {
                if (detail.getQuantityScaned() > 0) {
                    detail.setQuantityReceived(detail.getQuantityReceived() + detail.getQuantityScaned());
                    detail.setQuantityScaned(0);
                    transactionItemsMapper.updateByPrimaryKey(detail);
                    TransactionItems line = new TransactionItems();
                    String lotNumber = getLotNumber(transType, detail, dateNowStr1);
                    line.setLotNumber(lotNumber);
                    line.setTransactionId(tranId + "_ITEM_RECEIPT_" + dateNowStr);

                    line.setItemId(detail.getItemId());

                    int itemId = detail.getItemId();
                    int quantity = 0;
                    for (TransactionItemInventoryDetails tiid : tiids) {
                        if (itemId == tiid.getItemId()) {
                            quantity += tiid.getQuantity();
                        }
                    }
                    line.setQuantity(quantity);
                    line.setQuantityFulfilled(0);
                    line.setQuantityReceived(0);
                    line.setQuantityScaned(0);
//                    line.setBinNumber(detail.getBinNumber());
                    line.setAccountId(detail.getAccountId());
                    int result = transactionItemsMapper.insert(line);
                    if (result > 0) {
                        logger.info("transactionItems新增成功");
                    }
                }
            }

            for (TransactionItemInventoryDetails tiid : tiids) {
                TransactionItemInventoryDetails tmp = new TransactionItemInventoryDetails();
                tmp.setLotNumber(tiid.getLotNumber());
                tmp.setAccountId(tiid.getAccountId());
                tmp.setTransactionId(tranId + "_ITEM_RECEIPT_" + dateNowStr);
                tmp.setItemId(tiid.getItemId());
                tmp.setBinNumber(tiid.getBinNumber());
                tmp.setQuantity(tiid.getQuantity());
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.insert(tmp);
                logger.info("transactionInventoryDetails新增成功");
                //修改库存数量
                //commonService.updateItemInventory(location,tiid.getItemId(),tiid.getAccountId(),tiid.getBinNumber(),(tiid.getLotNumber()+dateNowStr1),tiid.getQuantity());
            }
            /*****利用线程发送ACTIVE_MQ模块*****/
            String transactionId = tranId + "_ITEM_RECEIPT_" + dateNowStr;
            WmsThread wms = new WmsThread("wms-mq", transactionId, transType, producer, "ITEM_RECEIPT", accountId);
            wms.start();
        }
    }

    private String getLotNumber(String transType, TransactionItems detail, String dateNowStr1) {
        String lotNumber = null;
        if ("SO_RETURN".equals(transType) || "TO".equals(transType)) {
            lotNumber = detail.getLotNumber();
        }
        if ("PO".equals(transType)) {
            //lotNumber = detail.getPlatform() + dateNowStr1;
              lotNumber = detail.getPlatform() + newNo.getNewNo();
        }
        return lotNumber;
    }


    private void createItemFulfillment(String transType, String tranId, List<TransactionItems> trsDetails, List<TransactionItemInventoryDetails> tiids, String accountId, Map request, User currentUser) {
        Transactions itemFulfillment = new Transactions();
        itemFulfillment.setCreatedFrom(tranId);
        itemFulfillment.setCreatedFromType(transType);
        itemFulfillment.setCreatedBy(currentUser.getUserName());
        itemFulfillment.setLastUpdateBy(currentUser.getUserName());
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateNowStr = sdf.format(d);
        itemFulfillment.setTranid(tranId + "_ITEM_FULFILLMENT_" + dateNowStr);
        itemFulfillment.setTransactionType("ITEM_FULFILLMENT");
        itemFulfillment.setAccountId(accountId);
        itemFulfillment.setSyncToNs("N");
        itemFulfillment.setNum(0);
        Map<String, Object> param = new HashMap();
        param.put("tranid", request.get("transactionId"));
        param.put("accountId", request.get("accountId"));
        Transactions trs = transactionsMapper.getTransactionByTranid(param);
        itemFulfillment.setLocationId(trs.getLocationId());
        itemFulfillment.setLocationName(trs.getLocationName());
        itemFulfillment.setStatus("COMPLETE");
        int resultCount = transactionsMapper.insert(itemFulfillment);
        if (resultCount > 0) {
            logger.info("transactions新增成功");
            for (TransactionItems detail : trsDetails) {
                if (detail.getQuantityScaned() > 0) {
                    detail.setQuantityFulfilled(detail.getQuantityFulfilled() + detail.getQuantityScaned());
                    detail.setQuantityScaned(0);
                    transactionItemsMapper.updateByPrimaryKey(detail);
                    TransactionItems line = new TransactionItems();
                    line.setTransactionId(tranId + "_ITEM_FULFILLMENT_" + dateNowStr);
                    line.setItemId(detail.getItemId());
                    int itemId = detail.getItemId();
                    int quantity = 0;
                    for (TransactionItemInventoryDetails tiid : tiids) {
                        if (itemId == tiid.getItemId()) {
                            quantity += tiid.getQuantity();
                        }
                    }

                    line.setQuantity(quantity);
                    line.setQuantityFulfilled(0);
                    line.setQuantityReceived(0);
                    line.setQuantityScaned(0);
//                    line.setBinNumber(detail.getBinNumber());
                    line.setAccountId(detail.getAccountId());
                    int count = transactionItemsMapper.insert(line);
                    if (count > 0) {
                        logger.info("transactionItems新增成功");
                    }
                }
            }
            for (TransactionItemInventoryDetails tiid : tiids) {
                TransactionItemInventoryDetails tmp = new TransactionItemInventoryDetails();
                tmp.setLotNumber(tiid.getLotNumber());
                tmp.setAccountId(tiid.getAccountId());
                tmp.setTransactionId(tranId + "_ITEM_FULFILLMENT_" + dateNowStr);
                tmp.setItemId(tiid.getItemId());
                tmp.setBinNumber(tiid.getBinNumber());
                tmp.setToBinNumber(tiid.getToBinNumber());
                tmp.setQuantity(tiid.getQuantity());
                tmp.setStatus("COMPLETE");
                transactionItemInventoryMapper.insert(tmp);
                logger.info("transactionInventoryDetails新增成功");
                //修改库存数量
                //出库库位数据做减法
                //commonService.updateItemInventory(trs.getLocationId(),tiid.getItemId(),tiid.getAccountId(),tiid.getBinNumber(),tiid.getLotNumber(),-tiid.getQuantity());
                //波次下架时是移库到打包库位
                if ("TO_WAVE".equals(transType) || "SO_WAVE".equals(transType)) {
                    //commonService.updateItemInventory(trs.getLocationId(),tiid.getItemId(),tiid.getAccountId(),tiid.getToBinNumber(),tiid.getLotNumber(),tiid.getQuantity());
                }
            }
            /*****利用线程发送ACTIVE_MQ模块*****/
            /*String transactionId = tranId+"_ITEM_FULFILLMENT_" + dateNowStr;
            WmsThreads.execute(new MsgProducer(), transactionId, transType, "ITEM_FULFILLMENT", accountId);
*/
            /*****利用线程发送ACTIVE_MQ模块*****/
            String transactionId = tranId + "_ITEM_FULFILLMENT_" + dateNowStr;
            WmsThread wms = new WmsThread("wms-mq", transactionId, transType, producer, "ITEM_FULFILLMENT", accountId);
            wms.start();
        }
    }

    /**
     * 获取单据提交明细清单
     * 包括
     * 还未结束扫码的SKU清单
     * 本次提交的SKU，库位，批次及数量
     * 已做过提交审核的SKU，库位，批次及数量
     */
    @Override
    public Map<String, Object> getTxSubmitDetail(String tranid, String transactionType, String accountId) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap(16);
        Map param = new HashMap();
        param.put("tranid", tranid);
        param.put("accountId", accountId);
        Map all = new HashMap();
        List<Map<String, Object>> notComplete;
        if ("SO_WAVE".equals(transactionType) || "TO_WAVE".equals(transactionType)) {
            notComplete = transactionItemsMapper.getItemsNotComplete(param);
        } else {
            notComplete = transactionItemsMapper.getItemsNotComplete2(param);
        }
        List<Map<String, Object>> complete = transactionItemsMapper.getItemsComplete(param);
        List<Map<String, Object>> submitted = transactionItemsMapper.getItemsSubmitted(param);
        all.put("notComplete", notComplete);
        all.put("complete", complete);
        all.put("submitted", submitted);
        map.put("result", all);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return map;
    }

    /**
     * 获取单据提交明细清单
     * 包括
     * 还未结束扫码的SKU清单
     * 本次提交的SKU，库位，批次及数量
     * 已做过提交审核的SKU，库位，批次及数量
     * TheSubmitted  本次提交
     * ToSubmit   待提交
     * Submitted  已提交审核的
     */
    @Override
    public Map<String, Object> getTxSubmitDetail2(String tranid, String transactionType, String accountId) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap(16);
        Map param = new HashMap();
        param.put("tranid", tranid);
        param.put("accountId", accountId);
        Map all = new HashMap();
        List<Map<String, Object>> notComplete;
        List<Map<String, Object>> ToSubmit;
        List<Map<String, Object>> Submitted;
        if ("SO_WAVE".equals(transactionType) || "TO_WAVE".equals(transactionType)|| "SO_RETURN".equals(transactionType)) {
            notComplete = transactionItemsMapper.getItemsThisTimeSubmit2(param);
            Submitted =  transactionItemsMapper.getItemsSubmitedToCheck2(param);
            ToSubmit = transactionItemsMapper.getItemsWaitSubmit2(param);
        } else {
            notComplete = transactionItemsMapper.getItemsThisTimeSubmit(param);
            Submitted = transactionItemsMapper.getItemsSubmitedToCheck(param);
            ToSubmit = transactionItemsMapper.getItemsWaitSubmit(param);
        }

        all.put("TheSubmitted", notComplete);
        all.put("ToSubmit", ToSubmit);
        all.put("Submitted", Submitted);

        map.put("result", all);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return map;
    }

    @Override
    public List<Transactions> searchErrorTransactions(Map<String, Object> param) {
        PageHelper.startPage((Integer) param.get("pageNum"), (Integer) param.get("pageSize"));
        List<Transactions> transactions = transactionsMapper.searchErrorTransactions(param);
        return transactions;
    }


}
